<template>
  <div class="app-main-container">
    <!-- <div class="app-main-title">首页广告</div> -->
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearCondition"
    />
    <div class="doumee-table-buttons">
      <el-button
        type="primary"
        @click="$router.push({ name: 'HomePageEdit' })"
        >{{ translate("add") }}</el-button
      >
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        :label="translate('adverTypes')"
        prop="name"
        min-width="120"
      >
        <template slot-scope="{ row }">
          <span v-if="row.type == 0">{{ translate("venueHomeBanner") }}</span>
          <span v-if="row.type == 1">{{ translate("xrgg") }}</span>
          <!-- <span v-if="row.type == 2">首页悬浮广告</span> -->
          <span v-if="row.type == 3">{{ translate("splb") }}</span>
          <span v-if="row.type == 4">{{ translate("shsylb") }}</span>
          <span v-if="row.type == 5">{{ translate("syjlbt") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('adverTitle')"
        min-width="150"
        prop="title"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('adverPic')"
        width="140"
      >
        <template slot-scope="scope">
          <el-image
            fit="contain"
            :src="scope.row.imageFullPath"
            :preview-src-list="[scope.row.imageFullPath]"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('gllx')"
        min-width="130"
        prop="typeName"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('glnr')"
        min-width="150"
        prop="paramName"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('status')"
        min-width="100"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isAllow"
            :active-value="'1'"
            :inactive-value="'0'"
            @change="stateChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('sort')"
        prop="sort"
        min-width="100"
      />
      <el-table-column
        align="center"
        :label="translate('createTime')"
        prop="createTime"
        min-width="160"
      />
      <el-table-column
        :label="translate('handle')"
        align="center"
        fixed="right"
        min-width="200"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-preventReClick
              type="text"
              @click="
                $router.push({
                  name: 'HomePageEdit',
                  query: { id: scope.row.id },
                })
              "
              >{{ translate("edit") }}</el-button
            >
            <el-button
              v-preventReClick
              type="text"
              style="color: red"
              @click="toDeleteBanner(scope.row.id)"
              >{{ translate("del") }}</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- <div class="flex-cc doumee-pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        hide-on-single-page
        :current-page.sync="pagination.page"
        @current-change="getList"
      />
    </div> -->
    <Pagination
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
  </div>
</template>

<script>
import { bannerDel, bannerList, bannerUpdateStatus } from "@/api";
import { parseTime } from "@/utils/index";
import { queryFormConfig } from "./config";
import Pagination from "@/components/paginations";
export default {
  name: "HomePageList",
  components: {
    Pagination,
  },
  data() {
    return {
      queryFormConfig,
      meta: [],
      list: [],
      listLoading: true,
      filters: {},
      pagination: {
        page: 1,
        rows: 10,
      },
      total: 0,
    };
  },
  created() {
    this.meta = this.$route.meta.buttons || [];
    this.getList(1);
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout();
    });
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout();
    });
    if (this.$route.query.renovate && this.$route.query.renovate === "1") {
      this.getList();
    }
  },
  methods: {
    clearCondition() {
      this.filters = {};
      this.getList(1);
    },
    handleSizeChange(value) {
      this.pagination.rows = value;
      this.getList(1);
    },
    getList(page) {
      this.listLoading = true;
      const { filters, pagination } = this;
      if (page) {
        pagination.page = page;
        this.pagination.page = page;
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date());
      }
      bannerList({
        pagination,
        param: { ...filters },
      })
        .then((res) => {
          this.listLoading = false;
          if (res.errorCode === "000000") {
            this.list = res.recordList;
            this.total = res.totalCount;
          }
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    stateChange(v) {
      const obj = { ...v };
      const arr = this.list;
      const that = this;
      this.listLoading = true;
      bannerUpdateStatus({
        param: {
          id: obj.id,
          isAllow: obj.isAllow,
        },
      })
        .then((res) => {
          if (res.errorCode === "000000") {
            if (obj.isAllow === "0") {
              that.$message.success(this.translate("jyScs"));
            } else {
              that.$message.success(this.translate("qyScs"));
            }
            that.getList(this.pagination.page);
          } else {
            arr.forEach((e) => {
              if (e.id === obj.id) {
                e.isAllow = obj.isAllow === "1" ? "0" : "1";
              }
            });
            that.list = arr;
            that.listLoading = false;
          }
        })
        .catch(() => {
          arr.forEach((e) => {
            if (e.id === obj.id) {
              e.isAllow = obj.isAllow === "1" ? "0" : "1";
            }
          });
          that.list = arr;
          that.listLoading = false;
        });
    },

    reSizePage(current, size, total) {
      // this.pagination.page, this.pagination.rows, this.total
      //   let currentPage;
      let totalPage = Math.ceil((this.total - 1) / this.pagination.rows);
      let currentPage =
        this.pagination.page > totalPage ? totalPage : this.pagination.page;
      this.pagination.page = currentPage < 1 ? 1 : currentPage;
      //   return currentPage;
    },

    toDeleteBanner(id) {
      this.$confirm(this.translate("bannerDelPlace"), this.translate("ts"), {
        confirmButtonText: this.translate("qd"),
        cancelButtonText: this.translate("cancel"),
      }).then(() => {
        bannerDel({
          param: { id },
        }).then((res) => {
          if (res.errorCode === "000000") {
            this.$message.success(this.translate("delScs"));
            let totalPage = Math.ceil((this.total - 1) / this.pagination.rows);
            let currentPage =
              this.pagination.page > totalPage
                ? totalPage
                : this.pagination.page;
            this.pagination.page = currentPage < 1 ? 1 : currentPage;
            this.getList(this.pagination.page);
          }
        });
      });
    },
  },
};
</script>
